<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="../dom/dom.js"></script>
    <script>
        let util = {
            create(proto){
                if(Object.create){
                    return Object.create(proto)
                }else{
                    let Proxy = function(){}
                    Proxy.prototype = proto
                    return new Proxy();
                }
            }
            //数据继承
            ,extend(target,...settings){
                //数据类型匹配器
                let matcher = {
                    base(key,value){
                        this[key] = value
                    }
                    ,object(key,value){
                        this[key] ? extendImpl(this[key],[value]) : this[key] = value
                    }
                    ,getType(value){
                        //注意
                        //typeof(['ck']) --> 'object'
                        return value != null && typeof(value) == 'object'? 'object':'base';
                    }
                }
                , extendImpl = function(target,settings){
                    settings.forEach(setting=>{
                        let keys = Object.keys(setting);
                        keys.forEach(key=>{
                            let value = setting[key]
                            , type = matcher.getType(value)
                            matcher[type].call(target,key,value);
                        })
                    })
                    return target;
                }
                return extendImpl(target,settings);
            }
        }

        let Ani = function(name){
            this.name = name;
        }

        Ani.prototype = {
            constructor:Ani
            ,sleep(){
                console.log("sleep of Ani")
            }
        }
        
        let Dog = function(name){
            //属性继承。
            Ani.call(this,name)
        }

        let inheritPrototype = function(Son,Father){
            //方法继承
            Son.prototype = util.create(Father.prototype)
            Son.prototype.constructor = Son;
        }

        inheritPrototype(Dog,Ani)

        util.extend(Dog.prototype,{
            wang(){
                return this.name+"在旺旺叫"
            }
        })

        //执行
        let dog = new Dog("泰迪")
        console.dir(dog)
        alert(dog.wang())
    </script>
</body>
</html>